//
//  HomeRecommendView.swift
//  HomeRecommendView
//
//  Created by Ben on 2021/8/19.
//

import SwiftUI

struct HomeRecommendView: View {
    @EnvironmentObject var homeVM: HomeViewModel
    
    @State private var recommendHeaderMinY: CGFloat = 0
    @State private var recommendHeaderHeight: CGFloat = 0
    @State private var recommendHeaderOffsetY: CGFloat = 0
    
    var body: some View {
        
        BenRefreshableScrollView {
            VStack(spacing: 0) {
               
                ZStack {
                    GeometryReader { proxy -> AnyView in
                        let frame = proxy.frame(in: .global)
                        DispatchQueue.main.async {
                            if recommendHeaderMinY == 0 {
                                recommendHeaderMinY = frame.minY
                            }
                            if recommendHeaderHeight != frame.height {
                                recommendHeaderHeight = frame.height
                            }
                            recommendHeaderOffsetY = recommendHeaderMinY - frame.minY

                            let scrollEnable = recommendHeaderOffsetY > recommendHeaderHeight
                            if homeVM.recommendGoodsListScrollEnable != scrollEnable {
                                homeVM.recommendGoodsListScrollEnable = scrollEnable
                            }
                        }
                        return AnyView(
                            Color.clear
                        )
                    }
                    
                    VStack(spacing: 10) {
                        // 闲鱼优品
                        HomeRecommendHightQualityView()
                            .padding(8)
                            .background(
                                RoundedRectangle(cornerRadius: 10)
                                    .fill(Color(hex: 0xF9FAFB))
                            )
                        
                        // 其他快捷入口
                        HomeRecommendQuickIntoView()
                            .padding(8)
                            .background(
                                RoundedRectangle(cornerRadius: 10)
                                    .fill(Color(hex: 0xF9FAFB))
                            )
                    }
                    .padding(.horizontal, 16)
                }
                
                HomeRecommendGoodsCategoryTabView()
                    .frame(width: SCREEN_WIDTH)
                    .offset(y: homeVM.recommendGoodsListScrollEnable ? recommendHeaderOffsetY - recommendHeaderHeight : 0)
                
                HomeRecommendGoodsListView()
                    .frame(width: SCREEN_WIDTH, height: SCREEN_HEIGHT - 280)

            }
            .frame(maxHeight: .infinity, alignment: .top)
        } onRefreshHeader: { complete in
            Task {

                guard let categoryId = homeVM.myChannelList[homeVM.homeRecommendCurrTabIndex].id else {
                    return
                }
                await homeVM.getGoodsCardListWith(categoryId: categoryId, maxTime: nil)
                complete()
            }
        } onRefreshFooter: { complete in
            Task {

                guard let categoryId = homeVM.myChannelList[homeVM.homeRecommendCurrTabIndex].id else {
                    return
                }
                guard let maxTime = homeVM.channelGoodsCardList["\(categoryId)"]?.last?.goodsInfo?.create_time else {
                    return
                }
                
                await homeVM.getGoodsCardListWith(categoryId: categoryId, maxTime: Int(maxTime))
                complete()
            }
        }
    }
}
